package com.WaVel.service.impl;

import com.Emplyment.entity.ResumeTemplate;
import com.Emplyment.result.Result;
import com.Emplyment.vo.ResumeTemplateDownAndPreviewVo;
import com.Emplyment.vo.ResumeTemplateVo;
import com.WaVel.service.ResumeTemplateService;
import com.WaVel.mapper.ResumeTemplateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ResumeTemplateServiceImpl implements ResumeTemplateService {

    @Autowired
    private ResumeTemplateMapper resumeTemplateMapper;

    @Override
    public List<ResumeTemplateVo> getResumeTemplate() {
        return resumeTemplateMapper.getResumeTemplate();
    }

    @Override
    public Result deleteTemplate(Integer id) {
        resumeTemplateMapper.deleteTemplate(id);
        return Result.success();
    }

    @Override
    public void uploadFile(MultipartFile previewFile, MultipartFile downloadFile, String imageUrl, String name) {
        try{
            // 1. 创建存储目录
            Path uploadDir = Paths.get("/YUN/模板存放地址");
            if (!Files.exists(uploadDir)) {
                Files.createDirectories(uploadDir);
            }

            //将文件转换成byte数组
            byte[] downloadFileContent = StreamUtils.copyToByteArray(downloadFile.getInputStream());
            String originalFilename = downloadFile.getOriginalFilename();
            Double size = Double.valueOf(downloadFile.getSize());
            BigDecimal fileSize = new BigDecimal(size/1024/1024);
            // 2. 设置保留两位小数，四舍五入模式
            fileSize = fileSize.setScale(2, RoundingMode.HALF_UP);

            // 3. 保存文件
            Path filePath = uploadDir.resolve(originalFilename);
            String fileExtension = StringUtils.getFilenameExtension(originalFilename);
            Files.copy(downloadFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

            //将pdf预览文件转换成byte数组
            byte[] previewFileContent = StreamUtils.copyToByteArray(previewFile.getInputStream());



            ResumeTemplate resumeTemplate = ResumeTemplate.builder()
                    .fileType(fileExtension)
                    .url("/YUN/模板存放地址/" + originalFilename)
                    .createTime(LocalDateTime.now().toString())
                    .name(name)
                    .size(fileSize+"MB")
                    .previewFile(previewFileContent)
                    .downFlie(downloadFileContent)
                    .photoUrl(imageUrl)
                    .build();


            resumeTemplateMapper.insertTemplate(resumeTemplate);

        }catch (Exception e){
            log.error("上传文件失败", e);
        }
    }

    //根据文件名查询文件
    @Override
    public ResumeTemplateDownAndPreviewVo ByIdGetResumeTemplate(Integer templateId) {
        ResumeTemplateDownAndPreviewVo resumeTemplate = resumeTemplateMapper.ByIdGetResumeTemplate(templateId);
        return resumeTemplate;
    }
}
